using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.XPath;
using Microsoft.Health;
using Microsoft.Health.ItemTypes;
using Microsoft.Health.Web;

namespace Microsoft.Health.Samples.PatientConnect.ContosoHealth.HealthVaultAdapter
{
    /// <summary>
    /// PushHealthVaultItemTypes class manages operation for uploading information from application to Health Vault
    /// </summary>
    public class PushHealthVaultItemTypes
    {
        private const string _CcdTypeId = "9c48a2b8-952c-4f5a-935d-f3292326bf54";
        private const string _CcrTypeId = "1e1ccbfc-a55d-4d91-8940-fa2fbf73c195";
        Guid _applicationId = new Guid();

        /// <summary>
        /// Constructor for Outbound class
        /// </summary>
        /// <param name="appId"></param>
        public PushHealthVaultItemTypes(Guid appId)
        {
            _applicationId = appId;
        }

        /// <summary>
        /// Uploads CCR document to Health Vault
        /// </summary>
        /// <param name="ccrDocumentPath">CCR document path</param>
        /// <param name="personId">Person Id</param>
        /// <param name="recordId">Record Id</param>
        public void UploadCcr(string documentPath,
                              Guid personId,
                              Guid recordId)
        {
            Guid ccrGuid=new Guid(_CcrTypeId);
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.Load(documentPath);
            HealthRecordAccessor accessor = new HealthRecordAccessor(HealthVaultConnectionManager.CreateConnection(_applicationId, personId), recordId);
            accessor.NewItem(new HealthRecordItem(ccrGuid, xmldocument));

        }

        /// <summary>
        /// Uploads Lab Result document to HealthVault
        /// </summary>
        /// <param name="labResultPath">Lab Result document file path</param>
        /// <param name="personId">Person Id</param>
        /// <param name="recordId">Record Id</param>
        public void UploadLabResults(string labResultPath,
                                    Guid personId,
                                    Guid recordId)
        {
            string labResultXpath = "lab-test-results/data-xml/lab-test-results";
            string extensionXpath = "lab-test-results/data-xml/common/additional-labgroup-data";
            XmlDocument xmldocument = new XmlDocument();
            xmldocument.Load(labResultPath);
            
            XmlDocument labDoc = new XmlDocument();
            labDoc.LoadXml(xmldocument.SelectSingleNode(labResultXpath).OuterXml);
            
            XPathNavigator navigator = xmldocument.CreateNavigator();

            HealthRecordAccessor accessor = new HealthRecordAccessor(HealthVaultConnectionManager.CreateConnection(_applicationId, personId), recordId);
            HealthRecordItem labItem = new HealthRecordItem(LabTestResults.TypeId, labDoc);

            HealthRecordItemExtension extension = new HealthRecordItemExtension("ContosoHealth");
            labItem.CommonData.Extensions.Add(extension);
            XPathNavigator extensionNavigator = extension.ExtensionData.CreateNavigator();
            extensionNavigator.InnerXml = navigator.SelectSingleNode(extensionXpath).InnerXml;

            accessor.NewItem(labItem);
        }

        /// <summary>
        /// Uploads CCD document to HealthVault
        /// </summary>
        /// <param name="ccdDocumentPath">CCD document file path</param>
        /// <param name="personId">Person Id</param>
        /// <param name="recordId">Record Id</param>
        public void UploadCcd(string documentPath,
                              Guid personId,
                              Guid recordId)
        {
            Guid ccdGuid =new Guid(_CcdTypeId);
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(documentPath);
            HealthRecordAccessor accessor = new HealthRecordAccessor(HealthVaultConnectionManager.CreateConnection(_applicationId, personId), recordId);
            accessor.NewItem(new HealthRecordItem(ccdGuid, xmlDocument));
        }
    }
}